package com.hbpu.encap.socket.homework;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Homework02Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket(InetAddress.getLocalHost(), 8888);

        try {
            System.out.println("开始发送消息：");
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bufferedWriter.write("hobby");
            bufferedWriter.newLine();
            bufferedWriter.flush();
            System.out.println("发送成功！");

            try {
                System.out.println("开始接收回复：");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String s = bufferedReader.readLine();
                System.out.println(s);
            } catch (IOException e) {
                System.out.println("接收消息时出现错误：" + e.getMessage());
            }
        } catch (IOException e) {
            System.out.println("消息发送失败：" + e.getMessage());
        }

    }
}
